package com.lemt.index.config;

import com.lemt.index.filter.AuthFilter;
import com.lemt.index.handler.AccessHandle;
import com.lemt.index.handler.AuthHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启在方法级别上的安全控制注解的支持
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthFilter authFilter;

    @Autowired
    private AccessHandle accessHandle;
    @Autowired
    private AuthHandle authHandle;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    /**
     * 允许访问
     */
    public static final String[] ALLOW_ASK = {
            "/user/login",
            "/user/register"
    };

    //允许访问的静态资源
    public static final String[] STATIC_ALLOW_ASK = {
            "/swagger**/**",
            "/doc.html",
            "/webjars/**",
            "/swagger-ui.html",
            "/swagger-resources/**",
            "/v2/api-docs**"
    };



    @Override
    public void configure(WebSecurity web) throws Exception {
        // 禁用Spring Security默认的登录和注销页面
        web.ignoring()
                .antMatchers("/login")
                .antMatchers("/logout")
                .antMatchers("/mall/**")
                .antMatchers("/common/**")
                .antMatchers(STATIC_ALLOW_ASK)
                .antMatchers(ALLOW_ASK)
                .antMatchers(HttpMethod.OPTIONS, "/**");
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.cors().and()
                .csrf().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS,"/**").permitAll()
                .antMatchers("/user/**").hasAnyAuthority("admin","user")
                .antMatchers("/user/admin/**").hasAuthority("admin")
                .antMatchers("/role/**").hasAnyAuthority("admin","user")
                .antMatchers("/role/admin").hasAuthority("admin")
                .antMatchers("/meau/**").hasAnyAuthority("admin","user")
                .antMatchers("/meau/admin").hasAuthority("admin")
                .anyRequest().authenticated();
        httpSecurity.addFilterBefore(authFilter, UsernamePasswordAuthenticationFilter.class);
        httpSecurity.exceptionHandling().accessDeniedHandler(accessHandle).authenticationEntryPoint(authHandle);
    }


}
